﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EyFramework.AutoUpdate
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main(string[] args)
        {
            Entity.StartArgs startArgs = PublicGlobal.GetStartUpArgs(args);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            AppDomain.CurrentDomain.AssemblyResolve += (sender, reargs) =>
            {
                String resourceName = "EyFramework.AutoUpdate." + new AssemblyName(reargs.Name).Name + ".dll";
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                {
                    Byte[] assemblyData = new Byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);
                    return Assembly.Load(assemblyData);
                }
            };
            //判断配置文件是否存在，
            string updateIni = Path.Combine(DirectoryUtil.GetCurrentDirectory(), "AutoUpdate.ini");
            IniFileUtil iniFile = new IniFileUtil(updateIni, false);
            if (!FileUtil.IsExistFile(updateIni))
            {
                FrmUpdateUrl frmUpdateUrl = new FrmUpdateUrl();
                if (frmUpdateUrl.ShowDialog() == DialogResult.OK)
                {
                    //保存配置文件
                    FileUtil.CreateFile(updateIni);
                    iniFile.IniWriteValue("AutoUpdate", "UpdateUrl", frmUpdateUrl.UpdateUrl);
                    iniFile.IniWriteValue("AutoUpdate", "AppVersion", "0.0.0");
                    PublicGlobal.UpdateUrl = frmUpdateUrl.UpdateUrl;
                    PublicGlobal.CurrentVersion = "0.0.0";
                }
                else
                {
                    return;
                }
            }
            else
            {
                string iUpdateUrl = iniFile.IniReadValue("AutoUpdate", "UpdateUrl");
                if (string.IsNullOrEmpty(iUpdateUrl))
                {
                    FrmUpdateUrl frmUpdateUrl = new FrmUpdateUrl();
                    if (frmUpdateUrl.ShowDialog() == DialogResult.OK)
                    {
                        iniFile.IniWriteValue("AutoUpdate", "UpdateUrl", frmUpdateUrl.UpdateUrl);
                        PublicGlobal.UpdateUrl = frmUpdateUrl.UpdateUrl;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    PublicGlobal.UpdateUrl = iUpdateUrl;
                }

                string iVersion = iniFile.IniReadValue("AutoUpdate", "AppVersion");
                if (string.IsNullOrEmpty(iVersion))
                {
                    iniFile.IniWriteValue("AutoUpdate", "AppVersion", "0.0.0");
                    PublicGlobal.CurrentVersion = "0.0.0";
                }
                else
                {
                    PublicGlobal.CurrentVersion = iVersion;
                }
            }
            Application.Run(new FrmUpdate(startArgs));
        }
    }
}